package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import entity.Properties;
import entity.PsPoleInfo;

@Repository("poleDao")
public class PoleDao<K, V> extends HibernateDaoSupport{
	public List<PsPoleInfo> selectAllPole(final int page,final int rows,final PsPoleInfo psPole){
		return this.getHibernateTemplate().execute(new HibernateCallback<List<PsPoleInfo>>() {
			public List<PsPoleInfo> doInHibernate(Session paramSession)
					throws HibernateException {
				Criteria c=paramSession.createCriteria(PsPoleInfo.class);
				if(psPole!=null){
					if(psPole.getLineid()!=0){
						c.add(Restrictions.eq("lineid", psPole.getLineid()));
					}if(psPole.getIsstart()!=0){
						c.add(Restrictions.eq("isstart", psPole.getIsstart()));
					}
				}
				return c.setFirstResult((page-1)*rows).setMaxResults(rows).list();
			}
		});
	}
	
	public List<Properties<K, V>> findDistinctPoleCode(){
		List<Properties<K, V>> list=new ArrayList<Properties<K, V>>();
		List l=this.getHibernateTemplate().find("select distinct id,polecode from PsPoleInfo");
		for (Object o : l) {		
			Object[] temp=(Object[])o;
			list.add(new Properties(temp[0],temp[1]));
		}
		return list;
	}
	
	public int count(){
		List<Long> count=(List<Long>)this.getHibernateTemplate().find("select count(0) from PsPoleInfo");
		return count.get(0).intValue();
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void changeIsStart(PsPoleInfo psPole) throws Exception{
		PsPoleInfo p=this.getHibernateTemplate().get(PsPoleInfo.class, psPole.getId());	
		if(p.getIsstart()==1){
			p.setIsstart((byte) -1);
		}else{
			p.setIsstart((byte) 1);
		}		
		
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void deletePole(final PsPoleInfo psPole)  throws Exception{
		this.getHibernateTemplate().delete(psPole);		
		
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void addUpdatePole(PsPoleInfo psPole)  throws Exception{
		
		if(psPole.getId()!=null){
			PsPoleInfo p=this.getHibernateTemplate().get(PsPoleInfo.class, psPole.getId());
			BeanUtils.copyProperties(psPole, p);
		}else{
			this.getHibernateTemplate().save(psPole);
		}
		
		
	}
	public PsPoleInfo findPole(PsPoleInfo psPole){
		return ((List<PsPoleInfo>)this.getHibernateTemplate().find("from PsPoleInfo where id=?",
				psPole.getId())).get(0);
		
	}
	public boolean findPoleCode(PsPoleInfo psPole){
		return ((List<PsPoleInfo>)this.getHibernateTemplate().find("from PsPoleInfo where polecode=?",
				psPole.getPolecode())).size()>0?true:false;
	}
}
